﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using MiniCrawler.Business;
using MiniCrawler.Business.Collectors.LinkCollectors;
using MiniCrawler.Business.Collectors.ResourceCollectors;
using MiniCrawler.DataAccess;
using MiniCrawler.DataAccess.Repositories;
using MiniCrawler.Entity;
using Thread = MiniCrawler.Entity.Thread;
using MiniCrawler.Downloader.ResourceDownloader;
using SystemThread = System.Threading.Thread;

namespace MiniCrawler.Form
{
    public partial class Form1 : System.Windows.Forms.Form
    {

        private Thread t1;
        private Thread t2;
        private Thread t3;
        private Thread t4;

        public delegate void UpdateLabelCallback(string text, Label label);
        public delegate void UpdateTextBoxCallback(string text, TextBox txtBox);

        public string[] filePath;
        public string[] filePath0;
        public string[] filePath1;
        public string[] filePath2;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           LoadThreadToList();
        }

        private void LoadThreadToList()
        {
            lstThread.DataSource = LoadAllThreadToTable();
            lstThread.DisplayMember = "ThreadName";
            lstThread.ValueMember = "ThreadId";
            lstThread.Update();
        }
        private Thread currentThread;
        private CategoryExpression currentCategoryExpression;
        private List<CategoryExpression> currentcExps;
        private DataExpression currentNewsExpression;


        public DataTable LoadAllThreadToTable()
        {
            ConfigHelper configHelper = new ConfigHelper();
            var threads = configHelper.GetAllThreads();

            var dtThread = new DataTable();
            dtThread.Columns.Add("ThreadName");
            dtThread.Columns.Add("ThreadId");

            if (threads != null)
            {
                foreach (var thread in threads.ToList())
                {
                    var newRow = dtThread.NewRow();
                    newRow["ThreadName"] = thread.Name;
                    newRow["ThreadId"] = thread.Id;

                    dtThread.Rows.Add(newRow);
                }
            }

            return dtThread;
        }


        public DataTable LoadAllCategoryToTable()
        {
            ConfigHelper configHelper = new ConfigHelper();
            var categoryExps = configHelper.GetCategoryExpressionesByThreadId(currentThread.Id);

            var dtThread = new DataTable();
            dtThread.Columns.Add("CategoryName");
            dtThread.Columns.Add("CategoryId");

            if (categoryExps != null)
            {
                foreach (var catExp in categoryExps.ToList())
                {
                    var newRow = dtThread.NewRow();
                    newRow["CategoryName"] = catExp.Name;
                    newRow["CategoryId"] = catExp.Id;

                    dtThread.Rows.Add(newRow);
                }
            }

            return dtThread;
        }


        public DataTable LoadAllDataToTable()
        {
            ConfigHelper configHelper = new ConfigHelper();
            var categoryExps = configHelper.GetDataExpressionesByThreadId(currentThread.Id);

            var dtThread = new DataTable();
            dtThread.Columns.Add("DataName");
            dtThread.Columns.Add("DataId");

            if (categoryExps != null)
            {
                foreach (var catExp in categoryExps.ToList())
                {
                    var newRow = dtThread.NewRow();
                    newRow["DataName"] = catExp.Name;
                    newRow["DataId"] = catExp.Id;

                    dtThread.Rows.Add(newRow);
                }
            }

            return dtThread;
        }

        public void LoadThread(string threadId)
        {
            int tId = 0;
            int.TryParse(threadId, out tId);

            ConfigHelper configHelper = new ConfigHelper();
            currentThread = configHelper.GetThreadById(tId);
        }

        public void LoadCategoryExpression(string categoryExpressionId)
        {
            int tId = 0;
            int.TryParse(categoryExpressionId, out tId);

            ConfigHelper configHelper = new ConfigHelper();
            var lstCategoryThreads = configHelper.GetCategoryExpressionesByThreadId(currentThread.Id);
            foreach (var categoryExpression in lstCategoryThreads)
            {
                if(categoryExpression.Id == tId)
                {
                    if(!string.IsNullOrEmpty(categoryExpression.UrlExpression))
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml(categoryExpression.UrlExpression);


                        XmlElement rootElement = doc.DocumentElement;
                        XmlElement nameElement = rootElement["Name"];
                        XmlElement sampleElement = rootElement["Sample"];
                        XmlElement enableElement = rootElement["Enabled"];


                        bool isEnabled = (enableElement != null && enableElement.Equals("1")) ? true : false;

                        XmlElement expressionParts = rootElement["ExpressionParts"];

                        txtExpression.Text = expressionParts.InnerXml.Replace("</Part>", "</Part>" + '\r' + '\n');
                        if (rootElement["Sample"] != null)
                            txtSample.Text = rootElement["Sample"].InnerText;
                    }


                    break;
                }
            }


        }

        public void LoadDataExpression(string dataExpressionId)
        {
            int tId = 0;
            int.TryParse(dataExpressionId, out tId);

            ConfigHelper configHelper = new ConfigHelper();
            var lstCategoryThreads = configHelper.GetDataExpressionesByThreadId(currentThread.Id);
            foreach (var categoryExpression in lstCategoryThreads)
            {
                if (categoryExpression.Id == tId)
                {
                    if (!string.IsNullOrEmpty(categoryExpression.UrlExpression))
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml(categoryExpression.UrlExpression);


                        XmlElement rootElement = doc.DocumentElement;
                        XmlElement nameElement = rootElement["Name"];
                        XmlElement sampleElement = rootElement["Sample"];
                        XmlElement enableElement = rootElement["Enabled"];


                        bool isEnabled = (enableElement != null && enableElement.Equals("1")) ? true : false;

                        XmlElement expressionParts = rootElement["ExpressionParts"];

                        txtDataExpression.Text = expressionParts.InnerXml.Replace("</Part>", "</Part>" + '\r' + '\n');
                        if (rootElement["Sample"] != null)
                            txtDataExpressionPattern.Text = rootElement["Sample"].InnerText;
                    }


                    break;
                }
            }


        }

        private void LoadCategoryToList()
        {
            if (currentThread != null)
            {
                lstCategoryExpression.DataSource = LoadAllCategoryToTable();
                lstCategoryExpression.DisplayMember = "CategoryName";
                lstCategoryExpression.ValueMember = "CategoryId";
                lstCategoryExpression.Update();
            }
        }


        private void LoadDataToList()
        {
            if (currentThread != null)
            {
                lstDataUrlExpression.DataSource = LoadAllDataToTable();
                lstDataUrlExpression.DisplayMember = "DataName";
                lstDataUrlExpression.ValueMember = "DataId";
                lstDataUrlExpression.Update();
            }
        }


        private void lstThread_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            LoadThread(lstThread.SelectedValue.ToString());

            LoadCategoryToList();

            LoadDataToList();
            if(currentThread != null)
            label1.Text = "Luồng hiện tại : " + currentThread.Name + " // Hiện không chạy";
        }

        private void lstCategoryExpression_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadCategoryExpression(lstCategoryExpression.SelectedValue.ToString());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LinkCollector collector = new LinkCollector(currentThread.Id);
            var t = new System.Threading.Thread(new ThreadStart(collector.Run));

            t.Name = "Thread Collector";

            if(currentThread != null && label1.Text.Contains("không chạy"))
            {
                t.Start();
                label1.Text = "Luồng hiện tại : " + currentThread.Name + " // Hiện đang chạy";
                return;
            }
            
            if (currentThread != null && label1.Text.Contains("đang chạy"))
            {
                t.Abort();
                label1.Text = "Luồng hiện tại : " + currentThread.Name + " // Hiện không chạy";
                return;
            }

            else
            {
                MessageBox.Show("Chưa chọn luồng !");
            }

            var m = t.ThreadState;

            var k = 0;
        }

        private void btnThemLuong_Click(object sender, EventArgs e)
        {
            ConfigHelper configHelper = new ConfigHelper();
            configHelper.AddNewThread(txtTenLuongMoi.Text, txtTrangChu.Text);
            LoadThreadToList();
        }

        private void btnThemChuyenMuc_Click(object sender, EventArgs e)
        {
            ConfigHelper configHelper = new ConfigHelper();

            string isEnabled = "<Enabled>" + (checkBox1.Checked ? 1 : 0) + "</Enabled>";
            string exp = "<Expression>" +
                         "<Name>" + txtTenChuyenMuc.Text + "</Name>" +
                         "<Sample>" + txtCauTruc.Text + "</Sample>" +
                         "<Enabled>" + (checkBox1.Checked ? 1 : 0) + "</Enabled>" +
                         "<ExpressionParts>" + txtPattern.Text + "</ExpressionParts>" +
                         "</Expression>";

            configHelper.AddNewCategory(currentThread, txtTenChuyenMuc.Text, exp);


            LoadCategoryToList();
        }

        private void btnAddTrangTin_Click(object sender, EventArgs e)
        {
            ConfigHelper configHelper = new ConfigHelper();

            string isEnabled = "<Enabled>" + (checkBox1.Checked ? 1 : 0) + "</Enabled>";
            string exp = "<Expression>" +
                         "<Name>" + txtTenTrangMoi.Text + "</Name>" +
                         "<Sample>" + txtTrangTinSample.Text + "</Sample>" +
                         "<Enabled>" + (chkTrangTinEnabled.Checked ? 1 : 0) + "</Enabled>" +
                         "<ExpressionParts>" + txtTrangTinExpression.Text + "</ExpressionParts>" +
                         "</Expression>";

            configHelper.AddNewData(currentThread, txtTenChuyenMuc.Text, exp);
            LoadDataToList();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int i = int.Parse(lstCategoryExpression.SelectedValue.ToString());
            ConfigHelper configHelper = new ConfigHelper();
            configHelper.DeleteCategory(i);
            LoadCategoryToList();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int i = int.Parse(lstCategoryExpression.SelectedValue.ToString());
            ConfigHelper configHelper = new ConfigHelper();

            var t = txtExpression.Text.Replace('\n', '_').Replace('\r', '_').Replace('\t', '_').Replace("_", "");

            string exp = "<Expression>" +
             "<Name>" + txtTenTrangMoi.Text + "</Name>" +
             "<Sample>" + txtTrangTinSample.Text + "</Sample>" +
             "<Enabled>" + (chkTrangTinEnabled.Checked ? 1 : 0) + "</Enabled>" +
             "<ExpressionParts>" + t + "</ExpressionParts>" +
             "</Expression>";
            configHelper.UpdateCategoryPattern(i, exp);
            
            LoadCategoryToList();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int i = int.Parse(lstDataUrlExpression.SelectedValue.ToString());
            ConfigHelper configHelper = new ConfigHelper();

            var t = txtDataExpression.Text.Replace('\n', '_').Replace('\r', '_').Replace('\t', '_').Replace("_", "");

            string exp = "<Expression>" +
             "<Name></Name>" +
             "<Sample></Sample>" +
             "<Enabled>" + "1" + "</Enabled>" +
             "<ExpressionParts>" + t + "</ExpressionParts>" +
             "</Expression>";

            configHelper.UpdateDataPattern(i, exp);

            LoadDataToList();
        }

        private void btnDeleteDataExpression_Click(object sender, EventArgs e)
        {
            int i = int.Parse(lstDataUrlExpression.SelectedValue.ToString());
            ConfigHelper configHelper = new ConfigHelper();
            configHelper.DeleteData(i);
            LoadDataToList();
        }

        private void lstDataUrlExpression_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDataExpression(lstDataUrlExpression.SelectedValue.ToString());
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {

            WallpaperHd collector = new WallpaperHd(currentThread.HostName, 2);
            var t = new System.Threading.Thread(new ThreadStart(collector.GetLinks));
            if (currentThread != null && label1.Text.Contains("không chạy"))
            {
                t.Start();
                label1.Text = "Download : " + currentThread.Name + " // Hiện đang chạy";
                return;
            }

            if (currentThread != null && label1.Text.Contains("đang chạy"))
            {
                t.Abort();
                label1.Text = "Download : " + currentThread.Name + " // Hiện không chạy";
                return;
            }

            else
            {
                MessageBox.Show("Chưa chọn luồng !");
            }

        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            WallpaperHdDownloader collector = new WallpaperHdDownloader();
            var t = new System.Threading.Thread(new ThreadStart(collector.Run));
            if (currentThread != null && label1.Text.Contains("không chạy"))
            {
                t.Start();
                label1.Text = "Download : " + currentThread.Name + " // Hiện đang chạy";
                return;
            }

            if (currentThread != null && label1.Text.Contains("đang chạy"))
            {
                t.Abort();
                label1.Text = "Download : " + currentThread.Name + " // Hiện không chạy";
                return;
            }

            else
            {
                MessageBox.Show("Chưa chọn luồng !");
            }
        }

        private void btnArrange_Click(object sender, EventArgs e)
        {
            var collector = new WallpaperHdArranger();
            var t = new System.Threading.Thread(new ThreadStart(collector.Run));
            if (currentThread != null && label1.Text.Contains("không chạy"))
            {
                t.Start();
                label1.Text = "Arranger : " + currentThread.Name + " // Hiện đang chạy";
                return;
               
            }

            if (currentThread != null && label1.Text.Contains("đang chạy"))
            {
                t.Abort();
                label1.Text = "Arranger : " + currentThread.Name + " // Hiện không chạy";
                return;
            }

            else
            {
                MessageBox.Show("Chưa chọn luồng !");
            }
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)
        {

            ModCloth collector = new ModCloth(currentThread.HostName, 2);
            var t = new System.Threading.Thread(new ThreadStart(collector.GetLinks));
            if (currentThread != null && label1.Text.Contains("không chạy"))
            {
                t.Start();
                label1.Text = "Download : " + currentThread.Name + " // Hiện đang chạy";
                return;
            }

            if (currentThread != null && label1.Text.Contains("đang chạy"))
            {
                t.Abort();
                label1.Text = "Download : " + currentThread.Name + " // Hiện không chạy";
                return;
            }

            else
            {
                MessageBox.Show("Chưa chọn luồng !");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ModCloth collector = new ModCloth(currentThread.HostName, 2);
            var t = new System.Threading.Thread(new ThreadStart(collector.FixUrls));
            if (currentThread != null && label1.Text.Contains("không chạy"))
            {
                t.Start();
                label1.Text = "Download : " + currentThread.Name + " // Hiện đang chạy FixLink";
                return;
            }

            if (currentThread != null && label1.Text.Contains("đang chạy"))
            {
                t.Abort();
                label1.Text = "Download : " + currentThread.Name + " // Hiện không chạy FixLink";
                return;
            }

            else
            {
                MessageBox.Show("Chưa chọn luồng !");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {

                var collector = new ModClothArranger();

                string resourcePath = "G:\\Downloads\\";

                var downloadLinks = DownloadLinkHelper.GetUndownloaded(currentThread.HostName);

                var p1 = new List<DownloadLink>();
                var p2 = new List<DownloadLink>();
                var p3 = new List<DownloadLink>();
                var p4 = new List<DownloadLink>();
                var p5 = new List<DownloadLink>();
                var p6 = new List<DownloadLink>();

                for (int i = 0; i < downloadLinks.Count; i++)
                {
                    if (i % 6 == 0) p1.Add(downloadLinks[i]);
                    if (i % 6 == 1) p2.Add(downloadLinks[i]);
                    if (i % 6 == 2) p3.Add(downloadLinks[i]);
                    if (i % 6 == 3) p4.Add(downloadLinks[i]);
                    if (i % 6 == 4) p5.Add(downloadLinks[i]);
                    if (i % 6 == 5) p6.Add(downloadLinks[i]);
                }

                var c1 = new ModClothArranger();
                c1.lstDownloadLinks = p1;
                var t1 = new System.Threading.Thread(new ThreadStart(c1.Run));


                var c2 = new ModClothArranger();
                c2.lstDownloadLinks = p2;
                var t2 = new System.Threading.Thread(new ThreadStart(c2.Run));


                var c3 = new ModClothArranger();
                c3.lstDownloadLinks = p3;
                var t3 = new System.Threading.Thread(new ThreadStart(c3.Run));


                var c4 = new ModClothArranger();
                c4.lstDownloadLinks = p4;
                var t4 = new System.Threading.Thread(new ThreadStart(c4.Run));


                var c5 = new ModClothArranger();
                c5.lstDownloadLinks = p5;
                var t5 = new System.Threading.Thread(new ThreadStart(c5.Run));

                var c6 = new ModClothArranger();
                c6.lstDownloadLinks = p6;
                var t6 = new System.Threading.Thread(new ThreadStart(c6.Run));

                if (currentThread != null && label1.Text.Contains("không chạy"))
                {
                    t1.Start();
                    t2.Start();
                    t3.Start();
                    t4.Start();
                    t5.Start();
                    t6.Start();

                    label1.Text = "Arranger : " + currentThread.Name + " // Hiện đang chạy";
                    return;

                }

                if (currentThread != null && label1.Text.Contains("đang chạy"))
                {
                    t1.Abort();
                    t2.Abort();
                    t3.Abort();
                    t4.Abort();
                    t5.Abort();
                    t6.Abort();
                    label1.Text = "Arranger : " + currentThread.Name + " // Hiện không chạy";
                    return;
                }

                else
                {
                    MessageBox.Show("Chưa chọn luồng !");
                }
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
            }
        }

        private void btnTestContent_Click(object sender, EventArgs e)
        {
            string url = "http://www.alibris.com/Chomp-Carl-Hiaasen/book/19315687?cm_sp=Home-_-gallery-_-slot1";

            var t = Common.GetWebPage(url);



            var collector = new LuuLLa(currentThread.HostName, 2);

            collector.GetAllLinks(t);

            
            var m = 0;
        }

        private void button6_Click(object sender, EventArgs e)
        {

            LuuLLa collector = new LuuLLa(currentThread.HostName, 2);
            var t = new System.Threading.Thread(new ThreadStart(collector.GetLinks));
            if (currentThread != null && label1.Text.Contains("không chạy"))
            {
                t.Start();
                label1.Text = "Download : " + currentThread.Name + " // Hiện đang chạy";
                return;
            }

            if (currentThread != null && label1.Text.Contains("đang chạy"))
            {
                t.Abort();
                label1.Text = "Download : " + currentThread.Name + " // Hiện không chạy";
                return;
            }

            else
            {
                MessageBox.Show("Chưa chọn luồng !");
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Alibris collector = new Alibris(currentThread.HostName, 2);
            var t = new System.Threading.Thread(new ThreadStart(collector.GetLinks));
            if (currentThread != null && label1.Text.Contains("không chạy"))
            {
                t.Start();
                label1.Text = "Download : " + currentThread.Name + " // Hiện đang chạy";
                return;
            }

            if (currentThread != null && label1.Text.Contains("đang chạy"))
            {
                t.Abort();
                label1.Text = "Download : " + currentThread.Name + " // Hiện không chạy";
                return;
            }

            else
            {
                MessageBox.Show("Chưa chọn luồng !");
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            LinkCollector collector = new LinkCollector(currentThread.Id);
            var t = new System.Threading.Thread(new ThreadStart(collector.RunGetBook));

            t.Name = "Thread Collector";

            if (currentThread != null && label1.Text.Contains("không chạy"))
            {
                t.Start();
                label1.Text = "Luồng hiện tại : " + currentThread.Name + " // Hiện đang chạy";
                return;
            }

            if (currentThread != null && label1.Text.Contains("đang chạy"))
            {
                t.Abort();
                label1.Text = "Luồng hiện tại : " + currentThread.Name + " // Hiện không chạy";
                return;
            }

            else
            {
                MessageBox.Show("Chưa chọn luồng !");
            }

            var m = t.ThreadState;

            var k = 0;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            NastyCloth collector = new NastyCloth(currentThread.HostName, 2);
            var t = new System.Threading.Thread(new ThreadStart(collector.GetLinks));
            if (currentThread != null && label1.Text.Contains("không chạy"))
            {
                t.Start();
                label1.Text = "Download : " + currentThread.Name + " // Hiện đang chạy";
                return;
            }

            if (currentThread != null && label1.Text.Contains("đang chạy"))
            {
                t.Abort();
                label1.Text = "Download : " + currentThread.Name + " // Hiện không chạy";
                return;
            }

            else
            {
                MessageBox.Show("Chưa chọn luồng !");
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {

            
            var abc = new string[]
                               {
                                  
                    "http://www.nastygal.com/clothes-swimwear-mix-match",
                    "http://www.nastygal.com/clothes",
                    "http://www.nastygal.com/clothes-dresses",
                    "http://www.nastygal.com/clothes-tops",
                    "http://www.nastygal.com/clothes-bottoms",
                    "http://www.nastygal.com/clothes-rompers-jumpsuits",
                    "http://www.nastygal.com/clothes-swimwear",
                    "http://www.nastygal.com/clothes-lingerie",
                    "http://www.nastygal.com/clothes-jackets-coats",
                    "http://www.nastygal.com/clothes-sweaters",
                    "http://www.nastygal.com/clothes-most-loved",
                    "http://www.nastygal.com/clothes_going-fast",
                    "http://www.nastygal.com/clothes-swimwear-bikinis",
                    "http://www.nastygal.com/clothes-swimwear-one-pieces",
                    "http://www.nastygal.com/clothes-back-in-stock",
                    "http://www.nastygal.com/clothes_exclusives",
                    "http://www.nastygal.com/clothes-dresses-48-under",
                    "http://www.nastygal.com/clothes-dresses-day",
                    "http://www.nastygal.com/clothes-dresses-going-out",
                    "http://www.nastygal.com/clothes-dresses-midi-maxi",
                    "http://www.nastygal.com/clothes-dresses-fit-n-flare",
                    "http://www.nastygal.com/clothes-dresses-body-con",
                    "http://www.nastygal.com/clothes-dresses-shift",
                    "http://www.nastygal.com/clothes_dresses_lbd",
                    "http://www.nastygal.com/clothes-dresses-solid",
                    "http://www.nastygal.com/clothes-dresses-basic",
                    "http://www.nastygal.com/clothes-tops-cropped",
                    "http://www.nastygal.com/clothes_tops_bustiers-bodysuits",
                    "http://www.nastygal.com/clothes-tops-tanks",
                    "http://www.nastygal.com/clothes-tops-tees",
                    "http://www.nastygal.com/clothes-tops-shirts-blouses",
                    "http://www.nastygal.com/clothes-tops-graphics",
                    "http://www.nastygal.com/clothes-tops-sweatshirts",
                    "http://www.nastygal.com/clothes-tops-basics",
                    "http://www.nastygal.com/clothes-outerwear-kimono",
                    "http://www.nastygal.com/clothes-bottoms-denim",
                    "http://www.nastygal.com/clothes-bottoms-skirts",
                    "http://www.nastygal.com/clothes-bottoms-shorts",
                    "http://www.nastygal.com/clothes-bottoms-pants",
                    "http://www.nastygal.com/clothes-bottoms-leggings-lounge",
                    "http://www.nastygal.com/clothes-intimates-bras-bralettes",
                    "http://www.nastygal.com/clothes-intimates-panties",
                    "http://www.nastygal.com/clothes-intimates-bustiers-bodysuits",
                    "http://www.nastygal.com/clothes-lingerie-sleep",
                    "http://www.nastygal.com/clothes-outerwear-jackets",
                    "http://www.nastygal.com/clothes-outerwear-coats",
                    "http://www.nastygal.com/clothes-outerwear-blazers",
                    "http://www.nastygal.com/clothes-outerwear-vests",
                    "http://www.nastygal.com/clothes_outerwear_moto",
                    "http://www.nastygal.com/clothes-outerwear-bomber",
                    "http://www.nastygal.com/clothes-sweaters-cardigans",
                    "http://www.nastygal.com/clothes-sweaters-cropped",
                    "http://www.nastygal.com/clothes-sweaters-pullover",
                    "http://www.nastygal.com/clothes_back-in-stock_dresses",
                    "http://www.nastygal.com/clothes_back-in-stock_tops",
                    "http://www.nastygal.com/clothes_back-in-stock_bottoms",
                    "http://www.nastygal.com/clothes-back-in-stock-lingerie",
                    "http://www.nastygal.com/clothes_back-in-stock_outerwear",
                    "http://www.nastygal.com/clothes_back-in-stock_swimwear",
            "http://www.nastygal.com/clothes_exclusives",
            "http://www.nastygal.com/clothes-most-loved",

        };

            var sampleLink = CategoryLinkHelper.SelectLinks("http://www.nastygal.com/", 1);


            foreach (var s in abc)
            {
                for(int i =0; i < 10; i++)
                {
                    var newLink = new CategoryLink
                                      {
                                          DateCreated = DateTime.Now,
                                          IsChecked = false,
                                          Level = 1,
                                          Value = s + "?seg=" + i,
                                          Website = "http://www.nastygal.com/",
                                          CategoryExpressionId = sampleLink[0].CategoryExpressionId
                    };
                    
                    CategoryLinkHelper.CreateLink(newLink);
                }


                for (int k = 0; k < 4; k++)
                {
                    var newLink = new CategoryLink
                    {
                        DateCreated = DateTime.Now,
                        IsChecked = false,
                        Level = 1,
                        Value = s + "?page=" + k,
                        Website = "http://www.nastygal.com/",
                        CategoryExpressionId = sampleLink[0].CategoryExpressionId
                    };

                    CategoryLinkHelper.CreateLink(newLink);
                }
            }
            

            
        }

        private void button11_Click(object sender, EventArgs e)
        {
            try
            {

                var collector = new ModClothArranger();

                string resourcePath = "G:\\Downloads\\";

                var downloadLinks = DownloadLinkHelper.GetDownloaded(currentThread.HostName);

                var p1 = new List<DownloadLink>();
                var p2 = new List<DownloadLink>();
                var p3 = new List<DownloadLink>();
                var p4 = new List<DownloadLink>();
                var p5 = new List<DownloadLink>();
                var p6 = new List<DownloadLink>();

                for (int i = 0; i < downloadLinks.Count; i++)
                {
                    if (i % 6 == 0) p1.Add(downloadLinks[i]);
                    if (i % 6 == 1) p2.Add(downloadLinks[i]);
                    if (i % 6 == 2) p3.Add(downloadLinks[i]);
                    if (i % 6 == 3) p4.Add(downloadLinks[i]);
                    if (i % 6 == 4) p5.Add(downloadLinks[i]);
                    if (i % 6 == 5) p6.Add(downloadLinks[i]);
                }

                var c1 = new ModClothArranger();
                c1.lstDownloadLinks = p1;
                var t1 = new System.Threading.Thread(new ThreadStart(c1.RunGetData));


                var c2 = new ModClothArranger();
                c2.lstDownloadLinks = p2;
                var t2 = new System.Threading.Thread(new ThreadStart(c2.RunGetData));


                var c3 = new ModClothArranger();
                c3.lstDownloadLinks = p3;
                var t3 = new System.Threading.Thread(new ThreadStart(c3.RunGetData));


                var c4 = new ModClothArranger();
                c4.lstDownloadLinks = p4;
                var t4 = new System.Threading.Thread(new ThreadStart(c4.RunGetData));


                var c5 = new ModClothArranger();
                c5.lstDownloadLinks = p5;
                var t5 = new System.Threading.Thread(new ThreadStart(c5.RunGetData));

                var c6 = new ModClothArranger();
                c6.lstDownloadLinks = p6;
                var t6 = new System.Threading.Thread(new ThreadStart(c6.RunGetData));

                if (currentThread != null && label1.Text.Contains("không chạy"))
                {
                    t1.Start();
                    t2.Start();
                    t3.Start();
                    t4.Start();
                    t5.Start();
                    t6.Start();

                    label1.Text = "Arranger : " + currentThread.Name + " // Hiện đang chạy";
                    return;

                }

                if (currentThread != null && label1.Text.Contains("đang chạy"))
                {
                    t1.Abort();
                    t2.Abort();
                    t3.Abort();
                    t4.Abort();
                    t5.Abort();
                    t6.Abort();
                    label1.Text = "Arranger : " + currentThread.Name + " // Hiện không chạy";
                    return;
                }

                else
                {
                    MessageBox.Show("Chưa chọn luồng !");
                }
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
            }
        }
    }
}
